/**
 * @author caojj
 * @version 1.0.0.5
 * @require 	FWK.include("FWK.Misc.coord");
 *				FWK.include("FWK.Util.Dragable");
 *
 */

 /* @usage
 
  			
 			var sld = new FWK.UI.Slider({
				bar:{className:"bar"},
				slider:{className:"slider"},
				step:5,
				defaultValue:40,
				max:255
			},{
				onmouseup:function(v) {
					$("val").innerHTML = v;
				}
			});
			sld.renderTo($("cot"));
 */

try {
	if(!FWK.UI){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.UI = {}
}


FWK.UI.Slider = function(initPara, callBackPara) {
	var me = this;
	
	this.bar = document.createElement("div");
	this.bar.style.position = "relative";
	this.slider = document.createElement("span");
//	this.slider.href = "javascript:;";
	this.slider.innerHTML = " ";
	this.slider.hideFocus = true;
	this.slider.style.outlineStyle = "none";
	
	this.bar.appendChild(this.slider);
	
	FWK.Util.Dragable.apply(this.slider, [this.slider, null, true, {scaleDom:this.bar, mode:"relative"}]);
	
	
	this.min = 0;
	this.max = 100;
	this.step = 1;
	this.defaultValue = 0;
	this.isVertical = false;
	
	if (initPara) {
		if (initPara.bar) {
			for(var p in initPara.bar) {
				if (p.search("style.") < 0) {
					this.bar[p] = initPara.bar[p];
				} else {
					this.bar.style[p.replace("style.", "")] = initPara.bar[p];
				}
			}
		}
		
		if (initPara.slider) {
			for(var p in initPara.slider) {
				if (p.search("style.")<0) {
					this.slider[p] = initPara.slider[p];
				} else {
					this.slider.style[p.replace("style.", "")] = initPara.slider[p];
				}
			}
		}
		
		for(var p in initPara) {
			if (typeof(initPara[p]) != "object") {
				this[p] = initPara[p];
			}
		}
	}
	
	if (callBackPara) {		
		this.slider.$doMove = function(){
			var val = me.getValue();
			if (callBackPara.onmousemove) {
				callBackPara.onmousemove.apply(me, [val]);
			}
		}	
	
		this.slider.$endDrag = function(){			
			var val = me.getValue();
			me.setValue(val);
			
			if (callBackPara.onmouseup) {
				callBackPara.onmouseup.apply(me, [val]);
			}			
		};
	
	
		this.slider.$startDrag = function() {
			if (callBackPara.onmousedown) {
				callBackPara.onmousedown.apply(me, [me.getValue()]);
			}
		};
		
	}
	
}

FWK.UI.Slider.prototype = {
	renderTo : function(cot) {
		cot.appendChild(this.bar);
		this.setValue(this.defaultValue);
	},
	getLen : function() {
		var barLen = 0;
		if (this.isVertical) {
			barLen = this.bar.offsetHeight - this.slider.offsetHeight;
		} else {
			barLen = this.bar.offsetWidth - this.slider.offsetWidth;
		}
		return barLen;
	},
	getPos : function() {
		var silderPos = 0;
		if (this.isVertical) {
			silderPos = FWK.getDomCoord(this.slider).y - FWK.getDomCoord(this.bar).y;	// + (this.slider.offsetHeight/2);
		} else {
			silderPos = FWK.getDomCoord(this.slider).x - FWK.getDomCoord(this.bar).x;	// + (this.slider.offsetWidth/2);
		}
		return silderPos;
	},
	getValue : function() {
//		debugger;
		var barLen = this.getLen();
		var silderPos = this.getPos();;
		
		var percent = silderPos / barLen;
		var val = percent * (this.max-this.min);
		
		val = this.min + Math.round(val / this.step) * this.step;
		//cos.innerHTML = this.slider.getAttribute("eventStatus");
		
		return val;
	},
	setValue : function(val) {
//		debugger;
		val = Number(val) || 0;
		val = Math.round((val-this.min) / this.step) * this.step;
		
		var percent = val / (this.max-this.min);
		var pos = percent * this.getLen();
		
		if (this.isVertical) {
			if (this.slider.style.position=="relative") {
				this.slider.style.top = pos + "px";
			} else {
				this.slider.style.top = pos + FWK.getDomCoord(this.bar).y + "px";
			}
		} else {
			if (this.slider.style.position=="relative") {
				this.slider.style.left = pos + "px";
			} else {
				this.slider.style.left = pos + FWK.getDomCoord(this.bar).x + "px";
			}
		}		
	}
}
